<template>
  <div class="online-sale">
    <div class="naviga">数据采集&nbsp;>&nbsp;售后数据</div>
    <div class="border-r margin-top">
      <div class="title">
        售后数据（<span class="blue">{{total}}</span>条）
        <span class="pull-right">更新时间： {{today}}</span>
      </div>
      <el-col :span="24" style="padding: 15px">
        <el-form :inline="true" :model="formInline" class="margin-top" style="width: 100%" label-position="right">
          <el-form-item label="归类">
            <el-select v-model="formInline.classifyId" placeholder="归类">
              <el-option label="全部" value="0"></el-option>
              <el-option
                v-for="item in classOptions"
                :key="item.classifyId"
                :label="item.classifyName"
                :value="item.classifyId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="提交时间">
            <el-date-picker
              v-model="date"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeDate"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="品类">
            <el-select v-model="formInline.categoryId" placeholder="品类" @change="modelList()">
              <el-option label="全部" value="0"></el-option>
              <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="型号">
            <el-select v-model="formInline.productCode" placeholder="型号">
              <el-option label="全部" value="ALL"></el-option>
              <el-option
                v-for="item in modelOptions"
                :key="item.productCode"
                :label="item.productName"
                :value="item.productCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getData">查询</el-button>
          </el-form-item>
        </el-form>
        <a class="el-button el-button--primary el-button--small" href="/file/download?fileName=售后数据模板.xlsx" download="售后数据模板" >下载模板</a>
        <el-upload
          style="display:inline-block;vertical-align: middle;height: 32px"
          class="avatar-uploader"
          action="/front/excelUpload?method=DataAc_API3020"
          name="file"
          :data="uploadConfig"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          accept=".xlsx,.xls">
          <el-button size="small" type="primary">导入数据</el-button>
        </el-upload>
        <el-button type="primary" size="small" @click="classManage()">归类管理</el-button>
      </el-col>
      <el-table
        :data="tableData"
        border
        style="width: 100%;margin-top: 15px">
        <el-table-column
          label="编号"
          prop="id"
          width="50">
        </el-table-column>
        <el-table-column
          label="主题"
          prop="mainContent"
          width="110">
        </el-table-column>
        <el-table-column
          label="详细描述"
          prop="description">
        </el-table-column>
        <el-table-column
          label="提交时间"
          prop="dataTime"
          width="160">
        </el-table-column>
        <el-table-column
          label="品类"
          prop="categoryName"
          width="110">
        </el-table-column>
        <el-table-column
          label="型号"
          prop="productCode"
          width="110">
        </el-table-column>
        <el-table-column
          label="归类"
          prop="classifyName"
          width="110">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button @click="showClass(scope.row.id)" type="text" size="small">归类</el-button>
            <el-button type="text" size="small" @click="showDetail(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding: 20px"
        background
        align="center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!--归类管理-->
    <div class="modal fade" id="classManage">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <div class="naviga">归类管理</div>
          </div>
          <div class="modal-body">
            <div class="row">
              <el-col :span="24">
                <div class="border-r">
                  <div class="title">
                    归类关键字列表
                    <el-button @click="newClass()" class="pull-right" type="text" size="small">新增</el-button>
                  </div>
                  <el-table
                    :data="classOptions"
                    border
                    style="width: 100%;margin-top: 15px">
                    <el-table-column
                      type="index"
                      label="序号"
                      width="110">
                    </el-table-column>
                    <el-table-column
                      label="归类关键字"
                      prop="classifyName"
                      width="110">
                    </el-table-column>
                    <el-table-column
                      label="来源"
                      prop="classifyType">
                    </el-table-column>
                    <el-table-column
                      fixed="right"
                      label="操作"
                      width="100">
                      <template slot-scope="scope">
                        <el-button  type="text" size="small" @click="editClass(scope.row.classifyId,scope.row.classifyName)">编辑</el-button>
                        <el-button type="text" size="small" @click="deleteClass(scope.row.classifyId)">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </el-col>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 归类 -->
    <div class="modal fade" id="classification">
      <div class="modal-dialog modal-dialog-big" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <div class="naviga">数据采集&nbsp;>&nbsp;售后数据&nbsp;>&nbsp;归类</div>
          </div>
          <div class="modal-body">
            <div class="row">
              <el-col :span="16">
                <div class="border-r">
                  <div class="title">售后数据信息</div>
                  <el-form ref="form" :model="form" label-width="80px" style="padding: 15px">
                    <el-form-item label="主题">
                      <el-input v-model="form.classifyName"  readonly="readonly"></el-input>
                    </el-form-item>
                    <el-form-item label="详细描述">
                      <el-input v-model="form.description"  type="textarea" rows="10" readonly="readonly"></el-input>
                    </el-form-item>
                  </el-form>
                  <table>
                    <tr>
                      <td class="left-td">附件</td>
                      <td>{{form.attachment}}</td>
                    </tr>
                  </table>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="border-r">
                  <div class="title">归类操作</div>
                  <el-form ref="form2" :model="form2" label-width="80px" style="padding: 15px">
                    <el-form-item label="归类">
                      <select v-model="form2.classification" class="form-control" style="width: 200px;display: inline-block" @change="changeClass()">
                        <option v-for="items in classOptions" :value="items">{{items.classifyName}}</option>
                      </select>
                      <el-button type="text" size="small" @click="newClass()">新建分类</el-button>
                    </el-form-item>
                    <el-form-item label="备注">
                      <el-input v-model="form2.classification.comment"  type="textarea" rows="10" readonly="readonly"></el-input>
                    </el-form-item>
                  </el-form>
                </div>
              </el-col>
            </div>
          </div>
          <div class="modal-footer">
            <div class="text-center">
              <el-button type="primary" @click="saveClass">确认</el-button>
              <button type="button" class="el-button el-button--info" data-dismiss="modal">取消</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 详情 -->
    <div class="modal fade" id="detail">
      <div class="modal-dialog modal-dialog-big" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <div class="naviga">数据采集&nbsp;>&nbsp;售后数据&nbsp;>&nbsp;详情</div>
          </div>
          <div class="modal-body">
            <div class="border-r">
              <div class="title">数据售后数据</div>
              <el-form ref="form" :model="form" label-width="80px" style="padding: 15px">
                <el-form-item label="主题">
                  <el-input v-model="form.mainContent" placeholder="请输入主题" readonly="readonly"></el-input>
                </el-form-item>
                <el-form-item label="详细描述">
                  <el-input v-model="form.description" placeholder="请输入详细描述" type="textarea" rows="10" readonly="readonly"></el-input>
                </el-form-item>
              </el-form>
              <table>
                <tr>
                  <td class="left-td">附件</td>
                  <td>{{form.attachment}}</td>
                </tr>
                <tr>
                  <td class="left-td">联系电话</td>
                  <td>{{form.creator}}</td>
                </tr>
                <tr>
                  <td class="left-td">提交时间</td>
                  <td>{{form.createTime}}</td>
                </tr>
                <tr>
                  <td class="left-td">状态更新时间</td>
                  <td>{{form.dataTime}}</td>
                </tr>
              </table>
            </div>
          </div>
          <div class="modal-footer">
            <div class="text-center">
              <el-button type="primary" data-dismiss="modal">确认</el-button>
              <button type="button" class="el-button el-button--info" data-dismiss="modal">取消</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="less" scoped>
  /*IE/7/6*/
  .blue {
    color: #03a9f3;
  }
  .title {
    height: 50px;
    line-height: 50px;
    padding: 0 10px;
    border-bottom: 1px solid #efefef;
  }
  .product-content {
    height: 60px;
    line-height: 20px;
    font-size: 14px;
    box-sizing: border-box;
    padding: 20px;
  }
  .modal-dialog-big{
    width: 100%;
    margin-top: 0;
    border-radius: 0;
  }
  .modal-body{
    background-color: #F6F6F6;
    padding-left: 0;
    padding-right: 0;
  }
  .left-td{
    width: 95px;
    text-align: right;
    padding-right: 10px;
  }
  .avatar-uploader .el-upload {
    border: none!important;
    border-radius: 0!important;
  }
</style>
<script type="es6">
  export default {
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();}
          },
        today: `1970-01-01 08:00:00`,
        // 归类
        classOptions:[],
        //提交时间
        date:  [],
        //品类
        categoryOptions: [],
        //产品型号
        modelOptions:[],
        //表格列表
        tableData: [],
        total: 0,
        pSize: 10,
        currentPage: 1,
        // 表单数据
        formInline: {
          startDate:'',
          endDate:'',
          classifyId:'0',
          categoryId:'0',
          productCode:'ALL',
          pageNo:1,
          pageSize:10
        },
        // 查看详情表单数据
        form: {},
        // 归类列表
        form2: {
          classification: ''
        },
        uploadConfig: {
          type: 'Document'
        },
      }
    },
    computed: {
      headers: function() {
        let token = sessionStorage.getItem("tokenName")
        return ({
          dipsToken: token,
        })
      }
    },
    mounted(){
      this.classList();
      this.categoryList();
      this.dataList();
      this.setDate();
    },
    methods: {
      // 时间范围改变时
      changeDate(val){
        this.formInline.startDate=val[0]
        this.formInline.endDate=val[1]
      },
      //页数
      handleSizeChange(val) {
        this.pSize = val;
        this.formInline.pageSize=val
        this.dataList();
      },
      handleCurrentChange(val) {
        this.formInline.pageNo=val
        this.currentPage = val
        this.dataList();
      },
      // 获取当前日期和当前日期前一个月
      formatDate(date) {
        var month=Number(date.getMonth()) + 1
        if(month<10){
          month='0'+month
        }
        return date.getFullYear()+'-'+month+'-'+date.getDate();
      },
      // 格式化日期
      tranTime(val){
        if(val < 10){
          return `0${val}`
        }else{
          return `${val}`
        }
      },
      setDate(){
        var nowdate = new Date();
        this.today= `${nowdate.getFullYear()}-${this.tranTime(nowdate.getMonth()+1)}-${this.tranTime(nowdate.getDate())} ${nowdate.getHours()}:${this.tranTime(nowdate.getMinutes())}:${this.tranTime(nowdate.getSeconds())}`;
        var startDate=this.formatDate(new Date(nowdate - 30*24*3600*1000))
        var endDate=this.formatDate(nowdate)
        this.formInline.startDate=startDate
        this.formInline.endDate=endDate
        this.date=[startDate,endDate]
        this.getData();
      },
      getData(){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API3010',
          query:this.formInline
        })
        .then((res) => {
          this.tableData=res.data.data.list;
          this.total=res.data.data.total
        })
        .catch((error) => {
          this.tableData=[]
          this.total=0
        });
      },
      // 1:归类下拉框
      classList(){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API3011'
        })
          .then((res) => {
            this.classOptions=res.data.data
          })
          .catch((error) => {});
      },
      // 2：获取品类下拉框
      categoryList(){
        this.$http.post(this.GLOBAL.host,{
          method:'getSubCategory'
        })
          .then((res) => {
            this.categoryOptions=res.data.data
          })
          .catch((error) => {});
      },
      // 3：根据品类获得型号列表
      modelList(){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API3012',
          "query":{
            "categoryId": this.formInline.category
          }
        })
          .then((res) => {
            this.modelOptions=res.data.data
          })
          .catch((error) => {});
      },
      // 4:获取到售后数据列表
      dataList(){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API3010',
          query:this.formInline
        })
          .then((res) => {
            this.tableData=res.data.data.list;
            this.total=res.data.data.total
          })
          .catch((error) => {});
      },
      // 5:显示详情,获取售后数据详情
      showDetail(id){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API3040',
          "query":{
            id: id
          }
        })
          .then((res) => {
            this.form=res.data.data
          })
          .catch((error) => {});
        $('#detail').modal('show')
      },
      // 6：归类管理
      classManage(){
        $('#classManage').modal('show')
      },
      //7：归类
      showClass(id){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API3040',
          "query":{
            id: id
          }
        })
          .then((res) => {
            this.form=res.data.data
            this.form2.id=id
            for(var i in this.classOptions){
              if(res.data.data.classifyId==this.classOptions[i].classifyId){
                this.form2.classification=this.classOptions[i];
              }
            }
          })
          .catch((error) => {});
        $('#classification').modal('show')
      },
      // 7.1
      // 修改当前选中的归类
      changeClass(){
        console.log(this.form2.classification)
        // this.form2.description=this.form2.classification
      },
      // 7.2保存修改后的归类信息
      saveClass(){
        this.$http.post(this.GLOBAL.host,{
          method:'DataAc_API30100',
          "query":{
            "id":this.form2.id ,
            "classifyId": this.form2.classification.classifyId,
            "classifyName":this.form2.classification.classifyName
          }
        })
          .then((res) => {
            if(res.data.code=='200'){
              this.$message({
                type: 'success',
                message: '保存成功: '
              });
              this.classList();
            }else{
              this.$message({
                type: 'error',
                message: '保存失败 '
              });
            }

          })
          .catch((error) => {
            this.$message({
              type: 'error',
              message: '新建失败: '
            });
          });
      },
      // 新建分类
      newClass(){
        this.$prompt('请输入分类名称', '新建分类', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^.*[^\s]+.*$/,
          inputErrorMessage: '请输入分类'
        }).then(({ value }) => {
          this.$http.post(this.GLOBAL.host,{
            method:'DataAc_API3090',
            "query":{
              "classifyName": value
            }
          })
            .then((res) => {
              if(res.data.code=='200'){
                this.$message({
                  type: 'success',
                  message: '新建成功: '
                });
                this.classList();
              }else{
                this.$message({
                  type: 'error',
                  message: '新建失败: '
                });
              }

            })
            .catch((error) => {
              this.$message({
                type: 'error',
                message: '新建失败: '
              });
            });


        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      },
      // 删除分类
      deleteClass(id){
        console.log(id)
        this.$confirm('此操作将删除分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(this.GLOBAL.host,{
            method:'DataAc_API30140',
            "query":{
              classifyId: id
            }
          })
            .then((res) => {
              if(res.data.data=='1'){
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                this.classList();
              }else{
                this.$message({
                  type: 'error',
                  message: '删除失败，不能删除，已被使用!'
                });
              }
            })
            .catch((error) => {
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            });

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      // 编辑分类
      editClass(id,name){
        this.$prompt('请输入分类名称', '编辑分类', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue:name,
          inputPattern: /^.*[^\s]+.*$/,
          inputErrorMessage: '请输入分类'
        }).then(({ value }) => {
          this.$http.post(this.GLOBAL.host,{
            method:'DataAc_API3090',
            "query":{
              classifyId:id,
              classifyName: value
            }
          })
            .then((res) => {
              if(res.data.code=='200'){
                this.$message({
                  type: 'success',
                  message: '编辑成功: '
                });
                this.classList();
              }else{
                this.$message({
                  type: 'error',
                  message: '编辑失败: '
                });
              }

            })
            .catch((error) => {
              this.$message({
                type: 'error',
                message: '编辑失败: '
              });
            });


        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      },
      // 导入数据
      handleAvatarSuccess(res) {
        if(res.code=='200'){
          this.$notify({
            title: '成功',
            message: '上传成功',
            type: 'success'
          });
          this.dataList()
        }else{
          this.$notify({
            title: '失败',
            message: '上传失败',
            type: 'error'
          });
        }
      }
    }
  };
</script>
